<? // create necessary files on the fly for log rotation (keeps up to date)

unset($domainslog);

// Generate log rotate for all domains
$dbp = mysql_query("SELECT host,domain,path FROM domains WHERE type='domain' OR type='subdomain'");
while ($data = mysql_fetch_array($dbp)) {
	$access_log = $data['path']."/".$data['host']."/logs/access";
	$error_log = $data['path']."/".$data['host']."/logs/error";
	$stats = $data['path']."/".$data['host']."/stats";
	$dom = $data['domain'];
	$host = $data['host'];
        // to ensure that apache sees the new log files we need a restart of apache.
        // the best restart method is a gracefull restart so we don't abort connections.
        // however if a gracefull is not possible we will do a hard restart.
        if($cfg['httpd_reload'] !== false) {
        	$apache_restart=$cfg['init']['httpd'].' reload';
        } else {
        	$apache_restart = $cfg['init']['httpd'].' restart';
        }
	$domainslog .= "$access_log {
        daily
        rotate 14
	prerotate
		".$cfg['prog']['webalizer']." -Q -p -n $host.$dom -o $stats -R 50 -S 50 -U 50 $access_log
	endscript
	missingok
# 	size 50M
	compress
	sharedscripts
	postrotate
		$apache_restart
	endscript
}

$error_log {
	daily
	rotate 14
	missingok
# 	size 10M
	compress
}

";
}

// add log rotate info for the webcp http server, restart both web servers
$serverlog = $cfg['basedir']."/httpd/logs/access ";
$serverlog .= $cfg['basedir']."/httpd/logs/access_log ";
$serverlog .= $cfg['basedir']."/httpd/logs/error ";
$serverlog .= $cfg['basedir']."/httpd/logs/error_log";
$serverlog .= " {
	daily
	rotate 14
	missingok
# 	size 50M
	compress
	sharedscripts
	postrotate
		".$cfg['init']['httpd-cp']." stop
		".$cfg['init']['httpd']." stop
		".$cfg['init']['httpd-cp']." start
		".$cfg['init']['httpd']." start
	endscript
}
";
if ($cfg['log_system'] == 'file' OR $cfg['log_system'] == 'filedb') {
$serverlog .= $cfg['logfile']." {
	missingok
	size 5M
	compress
	sharedscripts
}
";
}
// write file in logrotate.d dir
file_put_contents($cfg['logdir'].'webcp_domains',$domainslog.$serverlog);
?>
